﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
using SDCloudSolution.SDCloud.ModelRunAgent.Infrastructure;
using SDCloudSolution.SDCloud.ModelRunAgent.Infrastructure.Engines;

namespace SDCloud.ServiceAgentTests
{
    public class TestPysdSettingsProvider : IPysdServiceSettingsProvider
    {
        public string PythonExecutionPath => TestConsts.PythonExecutionPath;
        public string PythonExecutionArguments => TestConsts.PythonExecutionArguments;
        public string PythonServiceUrl => TestConsts.PythonServiceUrl;
        public int[] PythonServicePorts => TestConsts.PythonServicePorts;
        public int PythonServiceTimeout => TestConsts.PythonServiceTimeout;
    }


    [TestFixture]
    public class PySdRunnerTests
    {
        public PysdModelExecutor PysdModelExecutor;

        public static IEnumerable<string> GetAllSourceFiles()
        {
            var result = new List<string>();
            foreach (var directory in Directory.GetDirectories(TestConsts.ModelsRootFolder))
            {
                result.AddRange(Directory.GetFiles(directory)
                    .Where(file => file.EndsWith(".mdl")));
            }
            return result;
        }

        [SetUp]
        public void Setup()
        {
            PysdModelExecutor = new PysdModelExecutor(null, new TestPysdSettingsProvider());
        }



        [TestCaseSource(nameof(GetAllSourceFiles))]
        [TestMethod]
        public async Task PySdRunnerTest(string path)
        {
            try
            {
                var result = await PysdModelExecutor.ExecuteModel(path);
                NUnit.Framework.Assert.Pass("success");
            }
            catch (Exception ex)
            {
                NUnit.Framework.Assert.Fail(ex.Message);
            }
        }
    }
}
